/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.mdc.hearts.war.room;

import it.mdc.hearts.war.room.core.GameTable;
import it.mdc.hearts.war.room.core.UserInfo;
import java.io.Serializable;
import java.util.ArrayList;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import it.mdc.hearts.ejb.gameLogic.Card;
import org.primefaces.event.DragDropEvent;

/**
 *
 * @author Administrator
 */

@ManagedBean(name = "tableBean")
@ViewScoped
public class TableBean implements Serializable
{
    @ManagedProperty(value = "#{sessionUserBean}") // MY SESSION BEAN
    private SessionUserBean userSession;
    
    private boolean isPlayer;
    private Integer userID;
    private GameTable currentTable;
    private int tableStatus;    
    private int numPlayer;
    
    private ArrayList<Integer>[] cardhidden = (ArrayList<Integer>[])new ArrayList[4];
    private ArrayList<Card>[] playersDeckTaken;
    
    private ArrayList<Card> playerDeck;
    private ArrayList<Card> playedCard;
    private ArrayList<UserInfo> playerInfo;
    private ArrayList<UserInfo> viewerInfo;
    private int[] playersPoint;

    public boolean isIsPlayer() {
        isPlayer = currentTable.isPlayer(userID);
        return isPlayer;
    }
    
    public ArrayList<Card> getPlayerCard() 
    {
        playerDeck = (ArrayList<Card>)currentTable.getPlayerDeck(userID);
        return playerDeck;
    }

    public ArrayList<UserInfo> getPlayerInfo() {
        viewerInfo = currentTable.getPlayerInfo(userID);
        return viewerInfo;
    }
    
    public ArrayList<UserInfo> getViewerInfo() {
        playerInfo = currentTable.getViewerInfo();
        return playerInfo;
    }
    
    public ArrayList<Card> getPlayedCard() {
        playedCard = currentTable.getPlayedCard(userID);
        return playedCard;
    }

    public int getTableStatus() {
        tableStatus = currentTable.getTableStatus();
        return tableStatus;
    }    

    public ArrayList<Integer>[] getCardhidden() 
    {
        
        int[] numCards = currentTable.getPlayerCardNumber(userID);
      
        for(int i = 0; i < numCards.length; i++)
        {
            ArrayList<Integer> temp = new ArrayList<Integer>();
            for(int nCard = 0; nCard < numCards[i]; nCard++)
            {
                temp.add(nCard);
            }
            cardhidden[i] = temp;
        }
        
        return cardhidden;
    }

    public ArrayList<Card>[] getPlayersDeckTaken() {
        playersDeckTaken = currentTable.getPlayersDeckTaken(userID);
        return playersDeckTaken;
    }
    
    public int[] getPlayersPoint() {
        playersPoint = currentTable.getPlayersPoint(userID);
        return playersPoint;
    }

    public SessionUserBean getUserSession() {
        return userSession;
    }

    public void setUserSession(SessionUserBean userSession) {
        this.userSession = userSession;
    }

    @PostConstruct
    void initTableBean() 
    {
        userID = userSession.getID();
        currentTable = userSession.getUserInfo().getCurrentTable();
    }

    public int getNumPlayer() {
        numPlayer = currentTable.getAiPlayer() + currentTable.getNumPlayer();
        return numPlayer;
    }
    
    public void onCardDrop(DragDropEvent ddEvent) {
        //Card card = ((Card) ddEvent.getData());
        String cardItem = ddEvent.getDragId();
        String[] temp = cardItem.split(":");
        int index = Integer.parseInt(temp[1]);
        currentTable.playCard(userID, playerDeck.get(index));
    }
}
